﻿/*********************************************************************************************************************
VanguardDocumentation, Schema VanguardDocumentation

Creating a separate schema for VanguardDocumenation. Also creates a user which all procedures will run as (unless 
there is some specific need). This will give good separation from other parts of the database.

© 2014 Erik Ackerholm

This file is part of SqlVanguard.

SqlVanguard is free software: you can redistribute it and/or modify it under the terms of the GNU General Public 
License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later 
version.

SqlVanguard is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied 
warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with SqlVanguard. If not, see 
<http://www.gnu.org/licenses/>.
*********************************************************************************************************************/

if
	not exists (
		select *
		from sys.database_principals
		where name = 'VanguardDocumentation_SchemaUser'
			and [type] = 'S'
	)
begin
	create user VanguardDocumentation_SchemaUser
	without login;
end;
GO

if
	not exists (
		select *
		from sys.schemas
		where name = 'VanguardDocumentation'
	)
begin
	declare @SqlCommand as nvarchar(max);
	set @SqlCommand = 'create schema VanguardDocumentation authorization VanguardDocumentation_SchemaUser';
	execute (@SqlCommand);
end;
GO

-- Needs dbo to be able to view/change extended properties on all objects
execute sp_addrolemember 'db_owner', 'VanguardDocumentation_SchemaUser';
GO




